﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using WordNet.Core.ElementContracts;

namespace WordNet.Core
{
    public class IndexWordID: IIndexWordID
    {
        private const long serialVersionUID = 4683552775420575309L;
        protected readonly Regex _whitespace = new Regex("\\s+");
        private readonly String _lemma;
        private readonly POS _pos;


        public IndexWordID(String lemma, POS pos)
        {
            if (pos == null)
                throw new ArgumentNullException();
            lemma = lemma.ToLower().Trim();
            if (lemma.Length == 0)
                throw new ArgumentException();
            if (_whitespace.Match(lemma).Success)
                _lemma = lemma.Replace(_whitespace.Match(lemma).Value, "_");
            else
                _lemma = lemma;
            _pos = pos;
        }

        public POS PartOfSpeech
        {
            get { return _pos; }
        }

        public string Lemma
        {
            get
            {
                return _lemma;
            }
        }

        public override int GetHashCode()
        {
            const int PRIME = 31;
            int result = 1;
            result = PRIME * result + _lemma.GetHashCode();
            result = PRIME * result + _pos.GetHashCode();
            return result;
        }

        public override bool Equals(Object obj)
        {
            if (Object.ReferenceEquals(obj, null))
                return false;
            if (Object.ReferenceEquals(this, obj))
                return true;
            IIndexWordID other = obj as IIndexWordID;
            if (other == null)
                return false;
            
            if (!_lemma.Equals(other.Lemma))
                return false;
            if (!_pos.Equals(other.PartOfSpeech))
                return false;
            return true;
        }

        public String toString()
        {
            return String.Format("XID-{0}-{1}", _lemma, _pos.Tag);
        }
       
    }
}
